package com.zd.bms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.req.OrderDetailListReqVo;
import com.zd.bms.bean.vo.req.OrderListReqVo;
import com.zd.bms.bean.vo.resp.TicketOrderDetailRespVo;
import com.zd.bms.bean.vo.resp.TicketOrderRespVo;
import com.zd.bms.service.ITicketInfoService;
import com.zd.bms.service.ITicketOrderDetailService;
import com.zd.bms.service.ITicketOrderService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.bean.model.TicketOrder;
import com.zd.common.bean.model.TicketOrderDetail;
import com.zd.common.dao.common.Page;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Autowired
	private ITicketOrderService ticketOrderService;

	@Autowired
	private ITicketOrderDetailService ticketOrderDetailService;

	@Autowired
	private ITicketInfoService ticketInfoService;

	@RequestMapping("/index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		mv.addObject("bmsUser", bmsUser);
		mv.setViewName("/order/index");
		return mv;
	}

	@RequestMapping("/details")
	public ModelAndView details(Integer orderId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("result", ticketOrderService.findOrderDetails(orderId));
		mv.setViewName("/order/details");
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(OrderListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0) {
			reqVo.setScenicSpotId(scenicSpotId);
		}
		Page<TicketOrder> ticketOrderPage = ticketOrderService.pageTicketOrder(reqVo, bmsUser);

		List<TicketOrder> voList = ticketOrderPage.getList();
		List<TicketOrderRespVo> respVoList = new ArrayList<>();
		if (voList != null && voList.size() > 0) {
			for (TicketOrder ticketOrder : voList) {
				TicketOrderRespVo respVo = new TicketOrderRespVo();
				BeanUtils.copyProperties(ticketOrder, respVo);
				TicketInfo ticketInfo = ticketInfoService.get(ticketOrder.getTicketId());
				respVo.setTicketName(ticketInfo.getName());
				respVoList.add(respVo);
			}
		}
		resultMap.put("data", respVoList);
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", respVoList.size());
		resultMap.put("recordsFiltered", ticketOrderPage.getTotalRow());
		return resultMap;
	}

	@RequestMapping("/detailIndex")
	public ModelAndView detailIndex() throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		mv.setViewName("/order/detailIndex");
		mv.addObject("bmsUser", bmsUser);
		return mv;
	}

	@ResponseBody
	@RequestMapping("detailList")
	public Map<String, Object> detailList(OrderDetailListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0) {
			reqVo.setScenicSpotId(scenicSpotId);
		}
		Page<TicketOrderDetail> ticketOrderDetailPage = ticketOrderDetailService.pageTicketOrderDetail(reqVo);

		List<TicketOrderDetail> voList = ticketOrderDetailPage.getList();
		List<TicketOrderDetailRespVo> respVoList = new ArrayList<>();
		if (voList != null && voList.size() > 0) {
			for (TicketOrderDetail ticketOrderDetail : voList) {
				TicketOrderDetailRespVo respVo = new TicketOrderDetailRespVo();
				BeanUtils.copyProperties(ticketOrderDetail, respVo);
				TicketInfo ticketInfo = ticketInfoService.get(ticketOrderDetail.getTicketId());
				respVo.setTicketName(ticketInfo.getName());
				respVo.setPrices(ticketInfo.getPrice(),ticketInfo.getChildRate());
				respVoList.add(respVo);
			}
		}
		resultMap.put("data", respVoList);
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", respVoList.size());
		resultMap.put("recordsFiltered", ticketOrderDetailPage.getTotalRow());
		return resultMap;
	}

	@RequestMapping("statistics")
	public ModelAndView statistics() throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		mv.addObject("bmsUser", bmsUser);
		mv.setViewName("/order/staffIndex");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("staffList")
	public Map<String, Object> staffList(OrderListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0) {
			reqVo.setScenicSpotId(scenicSpotId);
		}
		Page<TicketOrder> ticketOrderPage = ticketOrderService.pageTicketOrder(reqVo, bmsUser);

		List<TicketOrder> voList = ticketOrderPage.getList();
		List<TicketOrderRespVo> respVoList = new ArrayList<>();
		if (voList != null && voList.size() > 0) {
			for (TicketOrder ticketOrder : voList) {
				TicketOrderRespVo respVo = new TicketOrderRespVo();
				BeanUtils.copyProperties(ticketOrder, respVo);
				TicketInfo ticketInfo = ticketInfoService.get(ticketOrder.getTicketId());
				respVo.setTicketName(ticketInfo.getName());
				respVoList.add(respVo);
			}
		}
		resultMap.putAll(ticketOrderService.statistics(reqVo, bmsUser));
		resultMap.putAll(ticketOrderDetailService.refundStatistics(reqVo, bmsUser));
		resultMap.put("data", respVoList);
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", respVoList.size());
		resultMap.put("recordsFiltered", ticketOrderPage.getTotalRow());
		return resultMap;
	}
	
	@RequestMapping("generateQrCode")
	public ModelAndView generateQrCode() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/order/generateQrCode");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("generateList")
	public Map<String, Object> generateList(Integer qrcodeNum) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderDetailService.batchQrcode(bmsUser, qrcodeNum));
		resultMap.put("result", true);
		return resultMap;
	}
}
